package com.software.loginregister.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.software.loginregister.R;
import com.software.loginregister.entity.vo.IngredientDTO;

import java.util.List;

public class IngredientAdapter extends RecyclerView.Adapter<IngredientAdapter.ViewHolder> {
    private List<IngredientDTO> ingredients;

    public IngredientAdapter(List<IngredientDTO> ingredients) {
        this.ingredients = ingredients;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_ingredient, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        IngredientDTO ingredient = ingredients.get(position);
        holder.tvName.setText(ingredient.getIngredientName());

        if (ingredient.getAmount() != null) {
            holder.tvAmount.setText(ingredient.getAmount().toString());
        } else {
            holder.tvAmount.setText("适量");
        }

        holder.tvUnit.setText(ingredient.getUnit());
    }
    // 添加获取单个食材的方法
    public IngredientDTO getIngredient(int position) {
        if (ingredients != null && position >= 0 && position < ingredients.size()) {
            return ingredients.get(position);
        }
        return null;
    }
    // 添加获取所有食材的方法
    public List<IngredientDTO> getAllIngredients() {
        return ingredients;
    }

    @Override
    public int getItemCount() {
        return ingredients != null ? ingredients.size() : 0;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvName;
        TextView tvAmount;
        TextView tvUnit;

        ViewHolder(View view) {
            super(view);
            tvName = view.findViewById(R.id.tv_ingredient_name);
            tvAmount = view.findViewById(R.id.tv_ingredient_amount);
            tvUnit = view.findViewById(R.id.tv_ingredient_unit);
        }
    }
}
